<?php
require_once '../config.php';

$account = operators_get_account();
$administratorId = $account['administratorId'];
$data = database_get_entity('provocations');

if (!empty($_GET['action']) && $_GET['action'] == 'add') {
  provocation_add($administratorId, $_POST['message'], $_POST['time'], $_POST['pages'], $_POST['url']);
  header('location: provocation.php');  
}
if (!empty($_GET['action']) && $_GET['action'] == 'update') {
  $operator = array_merge($operator, operator_get_by_id($_GET['id']));
  header('location: provocation.php');
}
if (!empty($_GET['action']) && $_GET['action'] == 'remove') {
  provocation_remove($_GET['id']);
  header('location: provocation.php');
}

$provocations = provocation_get_all();

include dirname(__FILE__) . '/header.php';
?>
  <table>
    <tr>
      <td id="Sidebar">
        <h2>Add new campaign</h2>
        <form action="?action=add" method="POST">
          <div>
            <label>Time on a site</label>
            <input type="text" name="time" value="<?php echo $data['time'] ?>" />
          </div>
          <div>                
            <label>Pages viewed</label>
            <input type="text" name="pages" value="<?php echo $data['pages'] ?>" />
          </div>
          <div>                
            <label>URL contains</label>
            <input type="text" name="url" value="<?php echo $data['url'] ?>" />
          </div>
          <div>                
            <label>Message</label>
            <input type="text" name="message" value="<?php echo $data['message'] ?>" />
          </div>
          <input type="submit" value="Add" />
        </form>
      </td>
      <td>
        <h2>Your campaigns</h2>
        <table class="Dg">
          <?php foreach ($provocations as $i): ?>
            <tr>
              <td class="DgCell">
                <a href="?action=update&id=<?php echo $i['id'] ?>"><?php echo $i['time'] ?></a>
              </td>
              <td class="DgCell">
                <a href="?action=update&id=<?php echo $i['id'] ?>"><?php echo $i['pages'] ?></a>
              </td>
              <td class="DgCell">
                <a href="?action=update&id=<?php echo $i['id'] ?>"><?php echo $i['url'] ?></a>
              </td>
              <td class="DgCell StringS IconCell">
                  <a href="?action=remove&id=<?php echo $i['id'] ?>"><img src="/common/images/trash_16x16.png" /></a>
              </td>
            </tr>
          <?php endforeach ?>
        </table>
        <br />
      </td>
    </tr>
  </table>
  

<?php include dirname(__FILE__) . '/footer.php' ?>
